import { UploadOutlined } from "@ant-design/icons";
import { Button, Upload } from "antd";
import { useRef, useState } from "react";
import "./index.less";
import * as XLSX from "xlsx";

export default ({
  value,
  onChange,
  showUploadList = false,
  loading = false,
  type = "不同应力",
}) => {
  const fileRef = useRef(null);
  const [fileNames, setFileNames] = useState([]);

  const beforeUpload = () => {
    let files = fileRef.current.files;
    const fileNames = Array.from(files).map((item) => item.name);
    setFileNames(fileNames);
    if (files.length === 0) return;

    const postDataArr = [];
    const fileReader = new FileReader();
    fileReader.onload = (e) => {
      const arrayBuffer = e.target.result;
      const workbook = XLSX.read(arrayBuffer, {
        type: "array",
        cellDates: true,
        cellText: false,
      });
      const worksheet = workbook.Sheets[workbook.SheetNames[0]];
      const data = XLSX.utils.sheet_to_json(worksheet, {
        header: 1,
        raw: false,
        dateNF: "yyyy-mm-dd", // 设置读取的日期格式 raw: false,cellDates: true, cellText: false,必须同时设置
      });
      console.log("data===", data);
      data.slice(1).forEach((arr) => {
        if (type === "不同应力") {
          postDataArr.push({
            name: arr[0],
            thickness: arr[1],
            a04: arr[2],
            weight: arr[3],
            density: arr[4],
            averageAperture: arr[5],
            averageRoundness: arr[6],
            averageArea: arr[7],
            areaDiameter: arr[8],
            bubbleDiameter: arr[9],
          });
        }

        if (type === "松弛") {
          postDataArr.push({
            name: arr[0],
            thickness: arr[1],
            strain40SourceLoad: arr[2],
            sourceLoad10: arr[3],
            sourceLoad191: arr[4],
            t10RelaxationRate: arr[5],
            t0RelaxationRate: arr[6],
            weight: arr[7],
            density: arr[8],
            averageAperture: arr[9],
            averageRoundness: arr[10],
            averageArea: arr[11],
            areaDiameter: arr[12],
            bubbleDiameter: arr[13],
          });
        }
      });
      if (type === "老化") {
        data.slice(2).forEach((arr) => {
          postDataArr.push({
            yl: arr[0],
            temperature: arr[1],
            name: arr[2],
            thickness2105: arr[3],
            weight: arr[4],
            density: arr[5],
            a042105: arr[6],
            thickness2310: arr[7],
            permanentDeformation: arr[8],
            a042310: arr[9],
            relaxationRate: arr[10],
            strainPresetPoint: arr[11],
            averageAperture: arr[12],
            averageRoundness: arr[13],
            averageArea: arr[14],
            areaDiameter: arr[15],
            bubbleDiameter: arr[16],
          });
        });
      }
      onChange(postDataArr, files[0]);
    };
    fileReader.readAsArrayBuffer(files[0]); // 通过文件上传组件选择的文件
  };

  return (
    <div className="upload-container">
      <Button loading={loading} icon={<UploadOutlined />}>
        上传文件
      </Button>
      {loading ? null : (
        <input
          className="add-input"
          type="file"
          ref={fileRef}
          multiple={true}
          onChange={beforeUpload}
        />
      )}

      {showUploadList && fileNames.length ? (
        <ul>
          {fileNames.map((item) => (
            <li key={item}>{item}</li>
          ))}
        </ul>
      ) : null}
    </div>
  );
};
